<?php
namespace Home\Controller;


class FoodmanController extends HomeController {
    //首页显示食材和口味推荐及统计数据
    public function index(){
        $food = M('food')->order('choose desc')->select();
        $taste = M('taste')->order('countnum desc')->select();
        $food123 = M('food')->order('choose desc')->limit(3)->select();
        $taste123 = M('taste')->order('countnum desc')->limit(3)->select();
        
        $food1 = $food123[0]['food'];
        $food2 = $food123[1]['food'];
        $food3 = $food123[2]['food'];
        $taste1 = $taste123[0]['taste'];
        $taste2 = $taste123[1]['taste'];
        $taste3 = $taste123[2]['taste'];

        $this->assign('food',$food);
        $this->assign('taste',$taste);
        $this->assign('food1',$food1);
        $this->assign('food2',$food2);
        $this->assign('food3',$food3);
        $this->assign('taste1',$taste1);
        $this->assign('taste2',$taste2);
        $this->assign('taste3',$taste3);
        $this->display();
    }
    //一键清除统计数据
    public function clear(){
        if($_POST['confirm']==1){
            $food = M('food')->where("choose > 0 ")->select();
            foreach ($food as $k => $v) {
                M('food')->where(array('id'=>$v['id']))->setField('choose',0);
            }
            $taste = M('taste')->where("countnum > 0 ")->select();
            foreach ($taste as $k => $v) {
                M('taste')->where(array('id'=>$v['id']))->setField('countnum',0);
            }
            //dump($taste);exit;
            $this->success("数据清空成功！",U('Foodman/index'));
        }
    }

    public function guanli(){
        $food = M('food')->select();
        $this->assign('food',$food);
        $this->display();
    }

    public function add(){
        $this->display();
    }

    //执行添加
    public function insert(){
            if(!D('Food')->create()){
                $this->error(D('Food')->getError());
                exit;
            }

            if(D('Food')->add() > 0){
                $this->success("添加成功！",U('Foodman/guanli'));
            }else{
                $this->error("添加失败！");
            }

    }

    public function del(){
        $id = $_GET['id'];
        if (M('food')->delete($id) > 0) 
        {
            $this->success("下架成功！",U('Foodman/guanli'));
        }else{
            $this->error("下架失败");
        }
    }
  
}